package bb.lanxing.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Property;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;

import bb.lanxing.R;

public class CircularProgress extends View {
    private static final long ANGLE_ANIMATOR_DURATION = 1500L;
    private static final int DEFAULT_BORDER_WIDTH = 3;
    private static final int MIN_SWEEP_ANGLE = 30;
    private static final long SWEEP_ANIMATOR_DURATION = 600L;
    private final RectF fBounds;
    private Property<CircularProgress, Float> mAngleProperty;
    private float mBorderWidth;
    private int[] mColors;
    private int mCurrentColorIndex;
    private float mCurrentGlobalAngle;
    private float mCurrentGlobalAngleOffset;
    private float mCurrentSweepAngle;
    private boolean mModeAppearing;
    private int mNextColorIndex;
    private ObjectAnimator mObjectAnimatorAngle;
    private ObjectAnimator mObjectAnimatorSweep;
    private Paint mPaint;
    private boolean mRunning;
    private Property<CircularProgress, Float> mSweepProperty;
    private static final Interpolator ANGLE_INTERPOLATOR = new LinearInterpolator();
    private static final Interpolator SWEEP_INTERPOLATOR = new AccelerateDecelerateInterpolator();

    public CircularProgress(Context context) {
        this(context, null);
    }

    public CircularProgress(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public CircularProgress(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.fBounds = new RectF();
        this.mModeAppearing = true;
        this.mAngleProperty = new Property<CircularProgress, Float>(Float.class, "angle") {
            @Override
            public Float get(CircularProgress circularProgress) {
                return circularProgress.getCurrentGlobalAngle();
            }

            @Override
            public void set(CircularProgress circularProgress, Float f) {
                circularProgress.setCurrentGlobalAngle(f);
            }
        };
        this.mSweepProperty = new Property<CircularProgress, Float>(Float.class, "arc") {
            @Override
            public Float get(CircularProgress circularProgress) {
                return circularProgress.getCurrentSweepAngle();
            }

            @Override
            public void set(CircularProgress circularProgress, Float f) {
                circularProgress.setCurrentSweepAngle(f);
            }
        };
        float f = context.getResources().getDisplayMetrics().density;
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.CircularProgress, i, 0);
        this.mBorderWidth = obtainStyledAttributes.getDimension(0, f * 3.0f);
        obtainStyledAttributes.recycle();
        int[] iArr = new int[4];
        this.mColors = iArr;
        iArr[3] = context.getResources().getColor(R.color.md_red_500);
        this.mColors[2] = context.getResources().getColor(R.color.md_yellow_500);
        this.mColors[0] = context.getResources().getColor(R.color.md_green_500);
        this.mColors[1] = context.getResources().getColor(R.color.md_light_blue_500);
        this.mCurrentColorIndex = 0;
        this.mNextColorIndex = 1;
        Paint paint = new Paint();
        this.mPaint = paint;
        paint.setAntiAlias(true);
        this.mPaint.setStyle(Paint.Style.STROKE);
        this.mPaint.setStrokeCap(Paint.Cap.ROUND);
        this.mPaint.setStrokeWidth(this.mBorderWidth);
        this.mPaint.setColor(this.mColors[this.mCurrentColorIndex]);
        setupAnimations();
    }

    private void start() {
        if (isRunning()) {
            return;
        }
        this.mRunning = true;
        this.mObjectAnimatorAngle.start();
        this.mObjectAnimatorSweep.start();
        invalidate();
    }

    public void stop() {
        if (!isRunning()) {
            return;
        }
        this.mRunning = false;
        this.mObjectAnimatorAngle.cancel();
        this.mObjectAnimatorSweep.cancel();
        invalidate();
    }

    private boolean isRunning() {
        return this.mRunning;
    }

    @Override
    protected void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (i == 0) {
            start();
        } else {
            stop();
        }
    }

    @Override
    protected void onAttachedToWindow() {
        start();
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        stop();
        super.onDetachedFromWindow();
    }

    @Override
    protected void onSizeChanged(int i, int i2, int i3, int i4) {
        super.onSizeChanged(i, i2, i3, i4);
        this.fBounds.left = (this.mBorderWidth / 2.0f) + 0.5f;
        this.fBounds.right = (i - (this.mBorderWidth / 2.0f)) - 0.5f;
        this.fBounds.top = (this.mBorderWidth / 2.0f) + 0.5f;
        this.fBounds.bottom = (i2 - (this.mBorderWidth / 2.0f)) - 0.5f;
    }

    @Override
    public void draw(Canvas canvas) {
        float f;
        super.draw(canvas);
        float f2 = this.mCurrentGlobalAngle - this.mCurrentGlobalAngleOffset;
        float f3 = this.mCurrentSweepAngle;
        if (this.mModeAppearing) {
            Paint paint = this.mPaint;
            int[] iArr = this.mColors;
            paint.setColor(gradient(iArr[this.mCurrentColorIndex], iArr[this.mNextColorIndex], f3 / 300.0f));
            f = f3 + 30.0f;
        } else {
            f2 += f3;
            f = (360.0f - f3) - 30.0f;
        }
        canvas.drawArc(this.fBounds, f2, f, false, this.mPaint);
    }

    private static int gradient(int i, int i2, float f) {
        float f2 = 1.0f - f;
        return Color.argb(255, (int) ((((16711680 & i2) >> 16) * f) + (((i & 16711680) >> 16) * f2)), (int) ((((65280 & i2) >> 8) * f) + (((i & 65280) >> 8) * f2)), (int) (((i2 & 255) * f) + ((i & 255) * f2)));
    }

    public void toggleAppearingMode() {
        boolean z = !this.mModeAppearing;
        this.mModeAppearing = z;
        if (z) {
            int i = this.mCurrentColorIndex + 1;
            this.mCurrentColorIndex = i;
            this.mCurrentColorIndex = i % 4;
            int i2 = this.mNextColorIndex + 1;
            this.mNextColorIndex = i2;
            this.mNextColorIndex = i2 % 4;
            this.mCurrentGlobalAngleOffset = (this.mCurrentGlobalAngleOffset + 60.0f) % 360.0f;
        }
    }

    private void setupAnimations() {
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(this, this.mAngleProperty, 360.0f);
        this.mObjectAnimatorAngle = ofFloat;
        ofFloat.setInterpolator(ANGLE_INTERPOLATOR);
        this.mObjectAnimatorAngle.setDuration(ANGLE_ANIMATOR_DURATION);//c.j
        this.mObjectAnimatorAngle.setRepeatMode(ValueAnimator.RESTART);
        this.mObjectAnimatorAngle.setRepeatCount(-1);
        ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(this, this.mSweepProperty, 300.0f);
        this.mObjectAnimatorSweep = ofFloat2;
        ofFloat2.setInterpolator(SWEEP_INTERPOLATOR);
        this.mObjectAnimatorSweep.setDuration(SWEEP_ANIMATOR_DURATION);
        this.mObjectAnimatorSweep.setRepeatMode(ValueAnimator.RESTART);
        this.mObjectAnimatorSweep.setRepeatCount(-1);
        this.mObjectAnimatorSweep.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
                CircularProgress.this.toggleAppearingMode();
            }
        });
    }

    public void setCurrentGlobalAngle(float f) {
        this.mCurrentGlobalAngle = f;
        invalidate();
    }

    public float getCurrentGlobalAngle() {
        return this.mCurrentGlobalAngle;
    }

    public void setCurrentSweepAngle(float f) {
        this.mCurrentSweepAngle = f;
        invalidate();
    }

    public float getCurrentSweepAngle() {
        return this.mCurrentSweepAngle;
    }
}